package xaj.lang;

import java.util.HashMap;
import java.util.Iterator;

import polyglot.ext.jl.ast.Node_c;
import polyglot.util.Position;

public class MapImpl
// extends Node_c
implements Map {

	private java.util.Map map;
	
	public MapImpl(/*Position p*/) {
//		super(p);
		map = new HashMap();
	}
	
	public MapImpl(/*Position p,*/ Map map) {
//		super(p);
		this.map = new HashMap();
		Iterator it = map.iterator();
		while(it.hasNext()) {
			String name = (String) it.next();
			this.map.put(name, map.get(name));
		}
	}
	
	// TODO
	// verificar se nao ha conflito de nomes
	public boolean add(String name, Object value) {
		if(!map.containsKey(name)) {
			if (value instanceof Map) {
				java.util.Map m = (java.util.Map) value;
				for (Object x : m.keySet()) {
					this.map.put(x, m.get(x));
				}
			}
			map.put(name, value);
			return true;
		} else
			return false;
	}

	public Object get(String name) {
		return map.get(name);
	}

	public Iterator iterator() {
		return map.keySet().iterator();
	}

	public void set(String name, Object value) {
		if(map.containsKey(name)) {
			map.keySet();
			map.put(name, value);
		}
	}

}

